package com.itboy.mall.sms.memberprice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.sms.memberprice.bo.MallMemberPriceAddBo;
import com.itboy.mall.sms.memberprice.bo.MallMemberPriceQueryBo;
import com.itboy.mall.sms.memberprice.bo.MallMemberPriceEditBo;
import com.itboy.mall.sms.memberprice.domain.MallMemberPrice;
import com.itboy.mall.sms.memberprice.mapper.MallMemberPriceMapper;
import com.itboy.mall.sms.memberprice.vo.MallMemberPriceVo;
import com.itboy.mall.sms.memberprice.service.IMallMemberPriceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品会员价格Service业务层处理
 *
 * @author itboy
 * @date 2021-08-08
 */
@Service
public class MallMemberPriceServiceImpl extends ServicePlusImpl<MallMemberPriceMapper, MallMemberPrice> implements IMallMemberPriceService {

    @Override
    public MallMemberPriceVo queryById(Long id){
        return getVoById(id, MallMemberPriceVo.class);
    }

    @Override
    public TableDataInfo<MallMemberPriceVo> queryPageList(MallMemberPriceQueryBo bo) {
        PagePlus<MallMemberPrice, MallMemberPriceVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallMemberPriceVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallMemberPriceVo> queryList(MallMemberPriceQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallMemberPriceVo.class);
    }

    private LambdaQueryWrapper<MallMemberPrice> buildQueryWrapper(MallMemberPriceQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallMemberPrice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuId() != null, MallMemberPrice::getSkuId, bo.getSkuId());
        lqw.eq(bo.getMemberLevelId() != null, MallMemberPrice::getMemberLevelId, bo.getMemberLevelId());
        lqw.like(StrUtil.isNotBlank(bo.getMemberLevelName()), MallMemberPrice::getMemberLevelName, bo.getMemberLevelName());
        lqw.eq(bo.getMemberPrice() != null, MallMemberPrice::getMemberPrice, bo.getMemberPrice());
        lqw.eq(bo.getAddOther() != null, MallMemberPrice::getAddOther, bo.getAddOther());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallMemberPriceAddBo bo) {
        MallMemberPrice add = BeanUtil.toBean(bo, MallMemberPrice.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallMemberPriceEditBo bo) {
        MallMemberPrice update = BeanUtil.toBean(bo, MallMemberPrice.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallMemberPrice entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
